package com.situ.springboot.service.impl;

import com.situ.springboot.mapper.BlogMapper;
import com.situ.springboot.pojo.entity.Blog;
import com.situ.springboot.pojo.vo.BlogVO;
import com.situ.springboot.service.IBlogService;
import com.situ.springboot.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

// Alt+Enter:快速修复一些错误
@Service
public class BlogServiceImpl implements IBlogService {
    @Autowired
    private BlogMapper blogMapper;

    //业务
    @Override
    public List<Blog> selectAll() {
        List<Blog> list = blogMapper.selectAll();
        return list;
    }

    @Override
    public void deleteById(Integer id) {
        blogMapper.deleteById(id);
    }

    @Override
    public void add(Blog blog) {
        blogMapper.add(blog);
    }

    @Override
    public Blog selectById(Integer id) {
        return blogMapper.selectById(id);
    }

    @Override
    public void update(Blog blog) {
        blogMapper.update(blog);
    }

    // totalCount:101
    // pageSize:10
    @Override
    public PageInfo selectByPage(Integer pageNo, Integer pageSize) {
        // limit
        int offset = (pageNo - 1) * pageSize;
        //查找当前也数据
        List<BlogVO> list = blogMapper.selectByPage(offset, pageSize);
        //查找总的数量，目的是为了算总的页数totalPage
        int totalCount = blogMapper.selectTotalCount();
        int totalPage = (int)Math.ceil((double) totalCount / pageSize);

        return new PageInfo<BlogVO>(list, totalPage, pageNo, pageSize);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        blogMapper.deleteAll(ids);
    }
}
